//
//  RJOutLineView.m
//  AirQuality
//
//  Created by Po on 15/7/3.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJOutLineView.h"
#import "RJOutLine.h"
@interface RJOutLineView ()
@property (retain, nonatomic) RJOutLine * lineView;                //折线图数据
@property (retain, nonatomic) NSDictionary * data;              //数据
@property (assign, nonatomic) CGFloat maxHeight;                //最大值
@property (assign, nonatomic) NSInteger yLabelWidth;            //纵轴宽度
@property (assign, nonatomic) BOOL isCO;
@property (assign, nonatomic) BOOL isCO1;
- (void)initInterface;
@end
@implementation RJOutLineView

- (instancetype)initWithFrame:(CGRect)frame data:(NSDictionary *)data succes:(BOOL)succes isCO:(BOOL)isCO1
{
    self = [super initWithFrame:frame];
    if (self) {
        if (data) {
//            NSString * key = [data[@"data"] allKeys][0];
            NSMutableArray * values = [data[@"data"] allValues][0];
            NSInteger count = 24 - values.count;
            while (count > 0) {
                [values addObject:@"0"];
                count--;
            }
            _data = data;
//            _isCO = [[[_data allValues] lastObject] isEqualToString:@"co"];
            _isCO = succes;
            _isCO1 = isCO1;
        }
            [self initInterface];
    }
    return self;
}

- (void)initInterface
{
    [self addSubview:[self getLineView]];
}

- (RJOutLine *)getLineView
{
    if (!_lineView) {
        CGRect rect = self.bounds;
        _yLabelWidth = DeviceType >= 6 ? 40 : 30;
        rect.origin.x += _yLabelWidth;
        rect.size.width -= _yLabelWidth;
        _maxHeight = [self getMaxHeightValue];
        if (_data.count != 0) {
            [self initYaxis];
        }
        _lineView = [[RJOutLine alloc] initWithFrame:rect data:_data maxHeight:_maxHeight];

    }
    return _lineView;
}

/**初始化Y轴*/
- (void)initYaxis
{
    CGFloat height = (self.bounds.size.height - 60) / 6;
    CGFloat labelHeight = height / 4 * 3;
    CGRect rect = CGRectMake(0,0, _yLabelWidth, labelHeight);
    for (NSUInteger i = 0; i < 7; i ++) {
        rect.origin.y = height * (i + 1) - height / 2 + 5;
        if (_isCO1) {
            CGFloat unit = _maxHeight / 6;               //平均每一段的值
            [self addSubview:[self getLabelWithFrame:rect title:[NSString stringWithFormat:@"%.2f",(unit  * (6 - i))]]];
            [self addSubview:[self getLabelWithFrame:CGRectMake(0,0, _yLabelWidth, height / 2 + 5) title:@"(mg)"]];
        } else {
            NSUInteger unit = _maxHeight / 6;               //平均每一段的值
            [self addSubview:[self getLabelWithFrame:rect title:[NSString stringWithFormat:@"%@",@(unit * (6 - i))]]];
            [self addSubview:[self getLabelWithFrame:CGRectMake(0,0, _yLabelWidth, height / 2 + 5) title:@"(ug)"]];
        }
    }
}

- (UILabel *)getLabelWithFrame:(CGRect)rect title:(NSString *)title
{
    UILabel * label = [[UILabel alloc] initWithFrame:rect];
    [label setText:title];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setTextColor:GetRGBColor(85, 85, 85, 1)];
    [label setFont:[UIFont systemFontOfSize:rect.size.height - 5]];
    return label;
}

- (CGFloat)getMaxHeightValue
{
    if (_data && _data.count != 0) {
        return [self getMaxNumberLine];
    }
    return 0;
}

- (CGFloat)getMaxNumberLine
{
    NSArray * allValue = [[_data[@"data"] allValues] lastObject];
    CGFloat maxNum = [allValue[0] floatValue];
    for (NSUInteger i = 0; i < [allValue count]; i ++) {
        CGFloat num = [allValue[i] floatValue];
        if (maxNum < num) {
            maxNum = num;
        }
    }
    
    //计算最贴近哪条线
    if (_isCO) {
        return ((NSUInteger)maxNum + 1);
    } else {
        return [self getMaxLineNumWithNum:maxNum];
    }
    return 0;
}

/**非CO的最大值计算*/
- (CGFloat)getMaxLineNumWithNum:(CGFloat)num
{
    NSUInteger temp = (NSUInteger)(num) + 50;
    if ((temp % 50) > 25) {
        return ((temp / 50) + 1) * 50;
    } else {
        return (temp / 50) * 50;
    }
}

@end
